package cn.yunhe.controller;

import cn.yunhe.pojo.Permission;
import cn.yunhe.pojo.ResultResponse;
import cn.yunhe.pojo.Role;
import cn.yunhe.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @RequestMapping("/queryUserRoles")
    public ResultResponse queryUserRoles(String uid){
        List<Role> roleList = roleService.queryUserRoles(uid);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(roleList);
        resultResponse.setCount((long) roleList.size());
        return resultResponse;
    }


    @RequestMapping("/queryAll")
    public ResultResponse queryAll(){
        List<Role> roleList = roleService.queryAll();
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(roleList);
        resultResponse.setCount((long) roleList.size());
        return resultResponse;
    }

    @RequestMapping("/insertRole")
    public ResultResponse insertRole(@RequestBody Role role){
        roleService.insertRole(role);
        return new ResultResponse();
    }

    @RequestMapping("/queryRolePermissions")
    public ResultResponse queryRolePermissions(String roleId){
        List<Permission> permissionList = roleService.queryRolePermissions(roleId);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setData(permissionList);
        resultResponse.setCount((long) permissionList.size());
        return resultResponse;
    }

    @RequestMapping("/insertRolePermissions")
    public ResultResponse insertRolePermissions(String roleId,String[] ids){
        roleService.insertRolePermissions(roleId, ids);
        return new ResultResponse();
    }
}
